import {
  Column,
  Entity,
  PrimaryGeneratedColumn,
  ViewColumn,
  ViewEntity,
} from 'typeorm';

export enum MediaTypes {
  // 文章
  ARTICLE = 'article',
  // 视频
  VIDEO = 'video',
  // 相册(使用轮播查看)
  GALLERY = 'gallery',
}

@Entity('tb_media', {
  comment: '媒体表',
})
export class Media {
  @PrimaryGeneratedColumn('increment', { comment: '自增主键', unsigned: true })
  id: number;

  @Column({ comment: '资源封面' })
  cover: string;

  @Column({ comment: '资源简介', nullable: true })
  description: string;

  @Column({ comment: '资源类型', default: MediaTypes.ARTICLE })
  type: MediaTypes;

  @Column({ comment: '资源内容', type: 'text' })
  content: string;

  @Column({ comment: '署名作者', nullable: true })
  author: string;

  @Column({ comment: '是否显示', type: 'tinyint', default: true })
  display: boolean;
}

//TODO 这里需要重新处理一下
@ViewEntity('tb_media_count', {
  expression: `
    SELECT "post"."id" AS "id", "post"."name" AS "name", "category"."name" AS "categoryName"
        FROM "post" "post"
        LEFT JOIN "category" "category" ON "post"."categoryId" = "category"."id"
  `,
})
export class MediaCountView {
  @ViewColumn({})
  likes: number;

  @ViewColumn({})
  favorites: number;

  @ViewColumn({})
  browse: number;

  @ViewColumn({})
  shares: number;

  @ViewColumn({})
  comments: number;
}
